module nehe.Lesson02;

import nehe.Lesson;
import nehe.Window;
import nehe.Vertex;

import derelict.opengl.gl;

import tango.util.collection.ArraySeq;

void main()
{
	//Create a scoped lesson instance
	scope Lesson lesson = new Lesson02();

	lesson.init();

	lesson.run();
}

public class Lesson02 : Lesson
{
	private ArraySeq!(Vertex) vertices;
	private ArraySeq!(Vertex) colors;
	
	public this()
	{
		vertices = new ArraySeq!(Vertex);
		colors = new ArraySeq!(Vertex);
	}
	
	public override void init()
	{
		window.create(800, 600, 32, "Ne(w)He Lesson 2 - Simple shapes");

		glShadeModel(GL_SMOOTH);                // Enable Smooth Shading
		glClearColor(0.0f, 0.0f, 0.0f, 0.5f);   // Black Background
		glClearDepth(1.0f);                     // Depth Buffer Setup
		glEnable(GL_DEPTH_TEST);                // Enables Depth Testing
		glDepthFunc(GL_LEQUAL);                 // The Type Of Depth Testing To Do

		// Now we set up the vertex and color data we want to draw to the screen

		//The first three Vertices represent a triangle
		vertices.append(Vertex(-1.5f, 1.0f, -6.0f));    // Top
		vertices.append(Vertex(-2.5f, -1.0f, -6.0f));    // Bottom Left
		vertices.append(Vertex(-0.5f, -1.0f, -6.0f));    // Bottom Right

		// We specify a color for every vertex, colors are float values for
		// Red Green Blue each ranging from 0.0 (nothing) to 1.0 (full intensity)
		colors.append(Vertex(1.0f, 0.0f, 0.0f));       // Red
		colors.append(Vertex(0.0f, 1.0f, 0.0f));        // Green
		colors.append(Vertex(0.0f, 0.0f, 1.0f));		// Blue

		// Next thing we want to draw is a quad:
		vertices.append(Vertex(0.5f, 1.0f, -6.0f));	// Top Left
		vertices.append(Vertex(2.5f, 1.0f, -6.0f));	// Top Right
		vertices.append(Vertex(2.5f, -1.0f, -6.0f));	// Bottom Right
		vertices.append(Vertex(0.5f, -1.0f, -6.0f));	// Bottom Left

		colors.append(Vertex(1.0f, 1.0f, 1.0f));       // White
		colors.append(Vertex(1.0f, 0.0f, 1.0f));        // Lila
		colors.append(Vertex(0.0f, 1.0f, 1.0f));		// Turquoise
		colors.append(Vertex(1.0f, 1.0f, 0.0f));		// Yellow
	}

	protected override void draw()
	{
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer
		glLoadIdentity();					// Reset The Current Modelview Matrix
		
		//Now we need to tell OpenGL to use the arrays we filled with data
		glEnableClientState(GL_VERTEX_ARRAY);   //We want a vertex array
		glEnableClientState(GL_COLOR_ARRAY);    //and a color array

		// All values are grouped to three Floats, we start at the beginning of the array (offset=0) and want to use 
		// vertices as VertexArray
		glVertexPointer(3, GL_FLOAT, 0, &vertices.toArray()[0]);   
		// Same here, but use colors
		glColorPointer(3, GL_FLOAT, 0, &colors.toArray()[0]);      

		glDrawArrays(GL_TRIANGLES, 0, 3); //We draw the first three vertices in the array as a triangle
		glDrawArrays(GL_QUADS, 3, 4);     //next 4 vertices are a quad (starting at the 3rd one)

		//Disable using the Vertex and Color array
		glDisableClientState(GL_COLOR_ARRAY);
		glDisableClientState(GL_VERTEX_ARRAY);
	}
}
